home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / beans / simplebean / example / Acme06Bean.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.0 KB  |  111 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. package acme.beans;
  18.  
  19. import java.awt.*;
  20. import java.io.Serializable;
  21.  
  22.  
  23. public class Acme06Bean extends Canvas implements Serializable {
  24.  
  25.  
  26.     public Acme06Bean() {
  27.         this("AcmeBean Serial# 06");
  28.     }
  29.  
  30.     public Acme06Bean(String label) {
  31.         super();
  32.         this.label = label;
  33.         setFont(new Font("Dialog", Font.PLAIN, 12));
  34.     }
  35.  
  36.     public synchronized void paint(Graphics g) {
  37.  
  38.         int width = getSize().width;
  39.         int height = getSize().height;
  40.  
  41.         g.setColor(beanColor);
  42.         g.fillRect(1, 1, width - 2, height - 2);
  43.         g.draw3DRect(0, 0, width - 1, height - 1, true);
  44.  
  45.         g.setColor(getForeground());
  46.         g.setFont(getFont());
  47.  
  48.         g.drawRect(2, 2, width - 4, height - 4);
  49.  
  50.         FontMetrics fm = g.getFontMetrics();
  51.         g.drawString(label, (width - fm.stringWidth(label)) / 2, 
  52.                           (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2);
  53.     }
  54.  
  55.     public Color getColor() {
  56.         return beanColor;
  57.     }
  58.  
  59.     public void setColor(Color newColor) {
  60.         beanColor = newColor;
  61.         repaint();
  62.     }
  63.  
  64.     public String getLabel() {
  65.         return label;
  66.     }
  67.  
  68.     public void setLabel(String newLabel) {
  69.         String oldLabel = label;
  70.         label = newLabel;
  71.         sizeToFit();
  72.     }
  73.  
  74.     public Dimension getPreferredSize() {
  75.         FontMetrics fm = getFontMetrics(getFont());
  76.         return new Dimension(fm.stringWidth(label) + TEXT_XPAD, 
  77.                              fm.getMaxAscent() + fm.getMaxDescent() + TEXT_YPAD);
  78.     }
  79.  
  80.     public Dimension getMinimumSize() {
  81.         return getPreferredSize();
  82.     }
  83.  
  84.     private void sizeToFit() {
  85.         Dimension d = getPreferredSize();
  86.         setSize(d.width, d.height);
  87.         Component p = getParent();
  88.         if (p != null) {
  89.             p.invalidate();
  90.             p.doLayout();
  91.         }
  92.     }
  93.  
  94.     public boolean handleEvent(Event evt) {
  95.         if (! isEnabled()) {
  96.             return false;
  97.         }
  98.         switch (evt.id) {
  99.         case Event.MOUSE_UP:
  100.             System.err.println("MOUSE_UP fired:");
  101.             return true;
  102.         }
  103.         return false;
  104.     }
  105.  
  106.     private Color beanColor = Color.cyan;
  107.     private String label;
  108.     static final int TEXT_XPAD = 12;
  109.     static final int TEXT_YPAD = 8;
  110. }
  111.